/*
 * c7_exercise7.cpp
 *
 *  Created on: Sep 2, 2013
 *      Author: cyper.yin
 */

#include <iostream>
using namespace std;

double * Fill_array(double * const p, int size);
void Show_array(const double * const p1, const double * const p2);
void Reverse_array(double * const p, double * const p2);
int main_c7e7(int argc, char **argv) {
	const int SIZE = 8;
	double arr[SIZE];

	double * p1 = arr;

	//fill array
	double * p2 = Fill_array(p1, SIZE);

	//show
	Show_array(p1, p2);

	//reverse all but the first and the last elements
	Reverse_array(p1 + 1, p2 - 1);

	//show again
	Show_array(p1, p2);

	return 0;
}

double * Fill_array(double * const p1, int size) {
	double * p = p1;
	do {
		cout << "Enter a double:";
		if (!(cin >> *(p))) {
			cin.clear();
			while (cin.get() != '\n') {
				continue;
			}
			break;
		}
	} while (++p < p1 + size);

	//tail
	return p-1;
}

void Show_array(const double * const p1, const double * const p2) {
	const double * p = p1;
	cout << "[";
	while (p <= p2) {
		cout << *p << " ";
		p++;
	}
	cout << "]" << endl;
}

void Reverse_array(double * const p1, double * const p2) {
	//pt1 point to the head
	double * pt1 = p1;

	//pt2 point to the tail
	double * pt2 = p2;

	while (pt1 < pt2) {
		double temp = *pt1;
		*pt1 = *pt2;
		*pt2 = temp;

		pt1++;
		pt2--;
	}
}
